package alf.mapping.view.activity;
import alf.mapping.dto.maps.Item;
import alf.mapping.dto.maps.NavigationLink;
import alf.mapping.view.Env;
import alf.mapping.view.Util;
import alf.mapping.view.activity.R;
import alf.mapping.view.visuals.EditorEngine;
import alf.mapping.view.visuals.ThemeManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Spinner;

public class ItemEditionActivity extends SimpleAbstractActivity {
	Item item;
	String oldType = null;
	String oldLink = null;
	EditorEngine editor;
	Spinner diagramSelector,itemSelector;
	String[] diagramNames,itemNames;

	private static final int REQUEST_IMAGE_SELECT = 1;
	private static final int REQUEST_ITEM_SELECT = 2;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.item_edit);
		editor = EditorEngine.getInstance();
		item = editor.getSelected().get(0).getItem();
		Util.setText(this, R.id.item_name, item.getName());
		oldType = item.getType();
		updateImage(oldType);
		Util.setText(this, R.id.item_text, item.getText());

		String linkText = "";
	
	//	Util.setText(this, R.id.item_navigationLink, linkText);
		oldLink = linkText;
    }

	private void updateImage(String type) {
		ImageView imv = (ImageView) this.findViewById(R.id.item_type_img);

		imv.setImageBitmap(editor.getBitmap(ThemeManager.SPRITES, type));
		Util.setText(this, R.id.item_type, type);
	}

	public void onImageClick(View v) {
		Intent i = new Intent(this, ImageSelectionActivity.class);
		i.setData(ImageSelectionActivity.prepareCall(ThemeManager.SPRITES,
													 Util.getText(this, R.id.item_type)));
		startActivityForResult(i, REQUEST_IMAGE_SELECT);
	}

	public void onOk(View v) {
		Intent i = getIntent();
		item.setName(Util.getText(this, R.id.item_name));
		item.setText(Util.getText(this, R.id.item_text));

		String newType = Util.getText(this, R.id.item_type);
		if (!newType.equals(oldType)) {
			item.setType(newType);
			editor.updateSprite(editor.getSelected().get(0));
		}

		setResult(RESULT_OK, i);
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
				case REQUEST_IMAGE_SELECT:
					if (resultCode == Activity.RESULT_OK) {
						String newType = data.getExtras().getString(Intent.EXTRA_RETURN_RESULT);
						updateImage(newType);
					}
					break;
			}
		}
	}

}
